package org.bzjj.servlet.login;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.bzjj.servlet.bean.ctrl.TuiBan;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String username = request.getParameter("username");
		String password = request.getParameter("password");

		HttpSession session = request.getSession();
		List<String> users = Arrays.asList(TuiBan.jyjg);

		if (username.equals("admin") && password.equals("kjk3361")) {
			session.setAttribute("username", username);
			session.setAttribute("role", "0");
			session.setAttribute("id", "0");
			response.sendRedirect("admin/index.jsp");
		} else if (users.contains(username) && password.equals("test")) {
			session.setAttribute("username", username);
			session.setAttribute("role", "1");
			session.setAttribute("id", users.indexOf(username)+"");
			response.sendRedirect("admin/index.jsp");
		} else {
			response.sendRedirect("err/error.jsp?code=loginFailed");
		}

	}
}
